import unittest
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

capabilities = dict(
    platformName='Android',
    automationName='uiautomator2',
    deviceName='Android',
    appPackage='com.android.settings',
    appActivity='.Settings',
    language='zh',
    locale='CN'
    # language='en',
    # locale='US'
)

appium_server_url = 'http://127.0.0.1:4723'

class TestAppium(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))

    def tearDown(self) -> None:
        if self.driver:
            print("完成！")

    def test_find_battery(self) -> None:

        settings = WebDriverWait(self.driver,10).until(EC.presence_of_element_located((AppiumBy.XPATH,'//android.widget.TextView[@text="设置"]')))
        settings.click()

        self.driver.swipe(500, 2185, 500, 600, 1000)

        print(self.driver.page_source)

        battery = self.driver.find_element(AppiumBy.XPATH,value='//android.widget.TextView[@text="电池"]')
        #battery = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((AppiumBy.XPATH,'//android.widget.TextView[@text="电池"]')))
        battery.click()


if __name__ == '__main__':
    unittest.main()